/*
 * @Author: along
 * @Date: 2020-06-01
 * @Last Modified by: along
 * @Last Modified time: 2020-06-01
 */
<template>
    <div class="container zdWebsitBrandChange">
        <el-form
            v-if="init"
            ref="mainform"
            :model="formData"
            label-position="right"
            status-icon
            :rules="formRule"
            label-width="110px"
            :disabled="false">
            <div class="form-unit">
                <div class="form-title">轮播图设置</div>
                <el-form-item
                    class="task-form-item"
                    size="small"
                    prop="title"
                    label="banner名称:">
                    <el-input
                        placeholder="必填"
                        v-model="formData.title"
                        size="small"
                        class="task-input"/>
                </el-form-item>
                <el-form-item
                    class="task-form-item"
                    size="small"
                    prop="pic_url"
                    label="轮播图图片:"
                >
                    <el-upload
                        :show-file-list="false"
                        :http-request="e=> {fnUploadImageOfCustomerLogo(e)}"
                        accept="image/*"
                        class="upload-demo"
                        drag
                        action="https://jsonplaceholder.typicode.com/posts/">
                        <img
                            v-if="formData.pic_url"
                            :src="formData.pic_url"
                            class="avatar">
                        <i
                            class="el-icon-upload"
                            v-if="!formData.pic_url"/>
                        <div
                            class="el-upload__text"
                            v-if="!formData.pic_url">将文件拖到此处，或<em>点击上传</em></div>
                        <div
                            class="el-upload__tip"
                            slot="tip">*只能上传jpg/png文件,banner尺寸大小1800*880px,且不超过500kb</div>
                    </el-upload>
                </el-form-item>
                <el-form-item
                    class="task-form-item"
                    size="small"
                    label="跳转链接:"
                >
                    <el-input
                        placeholder="跳转链接(选填)"
                        v-model="formData.link_url"
                        size="small"
                        class="task-input"
                    />
                </el-form-item>
                <el-form-item
                    class="task-form-item"
                    size="small"
                    prop="showTime"
                    label="展示时间:">
                    <el-date-picker
                        v-model="formData.showTime"
                        type="datetimerange"
                        range-separator="至"
                        size="small"
                        class="task-input"
                        start-placeholder="开始时间"
                        end-placeholder="结束时间" />
                    <span style="display:inline-block;color:#999;margin-left:12px">*结束时间为空则永久存在</span>
                </el-form-item>
                <el-form-item
                    class="task-form-item"
                    size="small"
                    prop="sort"
                    label="排序:">
                    <el-input
                        placeholder="必填"
                        v-model="formData.sort"
                        size="small"
                        prop="sort"
                        class="task-input"
                        type="number"
                    />
                    <span style="display:inline-block;color:#999;margin-left:12px">*数字越小排名越靠前</span>
                </el-form-item>
                <el-form-item
                    class="task-form-item"
                    size="small"
                    label="状态展示:">
                    <el-switch
                        v-model="formData.status"
                        active-color="#13ce66"
                        active-text="展示"
                        :active-value="1"
                        :inactive-value="0"
                        inactive-text="隐藏"/>
                </el-form-item>
            </div>
            <div class="form-unit">
                <div class="operate-baar">
                    <el-button
                        size="medium"
                        v-show="banner_id ? false : true"
                        @click="fnReset()">重置</el-button>
                    <el-button
                        size="medium"
                        type="primary"
                        @click="fnSubmit()">提交</el-button>
                </div>
            </div>
        </el-form>
    </div>
</template>

<script>
export default {
    components: {},
    data() {
        const validateArray = (rule, value, callback) => {
            if (!value) {
                callback(new Error(rule.message));
            } else {
                if (!value[0]) {
                    callback(new Error(rule.message));
                    return ;
                }
                callback();
            }
        };

        return {
            submitting: false,
            banner_id: '',
            formData: {
                title: "",
                pic_url: '',
                sort: '',
                showTime: [new Date().getTime(), ''],
                status: 1,
                link_url: ''
            },
            formRule: {
                pic_url: [
                    { required: true, message: '轮播图图片不能为空', trigger: 'change' }
                ],
                title: [
                    { required: true, message: '请输入标题', trigger: 'blur' },
                    { min: 1, max: 20, message: '长度在 1 到 20 个字', trigger: 'blur' }
                ],
                showTime: [
                    { required: true, message: '开始时间不可以为空', trigger: 'blur' },
                    { validator: validateArray, message: '请填写正确的时间', trigger: 'blur' }
                ],
                sort: [
                    { required: true, message: '请填写排序', trigger: 'blur' }
                ],
            },
            init: false,
            authResult: {}
        };
    },
    created() {
        this.authResult = this.$route.meta.authResult;
        this.$store.commit('SET_HEADER', [{ title: "APP后台"},{title: '官网'},{title: '官网自定义'},{title: '轮播图设置'}]);
        this.banner_id = this.$route.query.banner_id ? this.$route.query.banner_id : '';

        if (!this.banner_id) {
            this.init = true;
        }else{
            this.fnInitBannerInfo();
        }
    },
    methods: {
        /**
         * 获取轮播图数据
         */
        fnInitBannerInfo() {
            this.$post('/wei4/base%5Cbanner%5Cget', {
                id: this.banner_id
            }, resp => {
                if(resp.code == 1){
                    this.formData = {
                        ...resp.data,
                        showTime: [
                            resp.data.start_time == 0 ? '' : new Date(resp.data.start_time * 1000),
                            resp.data.end_time == 0 ? '' : new Date(resp.data.end_time * 1000)
                        ]
                    };
                    this.init = true;
                }else{
                    this.$message({
                        type: 'warning',
                        message: resp.msg
                    });
                }
            });
        },

        /**
         * 提交数据
         */
        fnSubmit(){
            if(this.submitting){
                return ;
            }
            this.$refs.mainform.validate(valid => {
                if(valid){
                    this.submitting = true;

                    console.log(this.formData, 'data');

                    let data = {
                        title: this.formData.title,
                        pic_url: this.formData.pic_url,
                        link_url: this.formData.link_url,
                        scope: 4,
                        start_time: this.formData.showTime.length > 0 ? (this.formData.showTime[0] == '' ? '' : new Date(this.formData.showTime[0]).getTime() / 1000) : '',
                        end_time : this.formData.showTime.length > 0 ? (this.formData.showTime[1] == '' ? '' : new Date(this.formData.showTime[1]).getTime() / 1000) : '',
                        sort: this.formData.sort,
                        status: this.formData.status,
                    };


                    if(this.banner_id){
                        data.id = this.banner_id;

                        this.$post('/wei4/base%5Cbanner%5Cedit', data, resp => {
                            if(resp.code == 1){
                                this.$message({
                                    type: 'success',
                                    center: true,
                                    message: '修改成功'
                                });
                                this.$router.push({
                                    name: 'zdBanner'
                                });
                            }else{
                                this.$message({
                                    type: 'warning',
                                    center: true,
                                    message: resp.msg
                                });
                            }
                            this.submitting = false;
                        });
                    }else{
                        this.$post('/wei4/base%5Cbanner%5Cadd', data, resp => {
                            if(resp.code == 1){
                                this.banner_id = resp.data.task_no;
                                this.$message({
                                    type: 'success',
                                    center: true,
                                    message: '操作成功'
                                });
                                this.$router.push({
                                    name: 'zdBanner'
                                });
                            }else{
                                this.$message({
                                    type: 'warning',
                                    center: true,
                                    message: resp.msg
                                });
                            }
                            this.submitting = false;
                        });
                    }

                }
            });
        },

        /**
         * 上传图片
         */
        fnUploadImage(e, success, error) {
            let timeTmp = new Date().getTime();
            let pic = new FormData();
            let size = e.file.size;
            let m = Math.ceil(size / 1000) < 1000 ? Math.ceil(size / 1000) + 'kb' : (size / 1000000).toFixed(2)*1 + 'MB';

            if(size > 500000) {
                this.$message({
                    type: 'warning',
                    message: `图片尺寸大小建议小于500kb,当前图片大小${m}`,
                    center: true,
                    duration: 3000
                });
            }

            pic.append('file', e.file);
            this.$upload({
                url: '/__local/common%5Cupload',
                data: pic,
                success: resp => {
                    if (resp.code == 1) {
                        success(resp);
                    } else {
                        this.$message({
                            type: 'warning',
                            message: '上传图片失败',
                            center: true
                        });
                        error(resp);
                    }
                }
            });
        },

        fnUploadImageOfCustomerLogo(e) {
            this.fnUploadImage(e, (resp) => {
                this.formData.pic_url = resp.data.url;
            }, (resp) => {
                console.log(resp.msg);
            });
        },

        /**
         * 重置
         */
        fnReset(){
            this.$refs.mainform.resetFields();
        }
    }
};
</script>

<style lang="less" scoped>
    .avatar{
        width: 100%;
        height: 100%;;
    }
    .margin-top{
        margin-top: 15px;
    }
    .zdWebsitBrandChange {
      background: #fff;
      margin: 15px;
      margin-bottom: 0;
      padding: 30px;
      overflow: auto;
    }
    .form-unit {
      margin-bottom: 40px;
    }
    .form-title {
      font-size: 16px;
      font-weight: 600;
      color: rgba(0, 0, 0, 0.85);
      line-height: 18px;
      padding: 0 10px;
      border-left: 2px solid #1890ff;
      margin-bottom: 20px;
    }
    .task-input {
      width: 435px !important;
    }
    input::-webkit-outer-spin-button,
    input::-webkit-inner-spin-button {
      -webkit-appearance: none;
    }
    input[type="number"] {
      -moz-appearance: textfield;
    }

    .operate-baar {
      width: 100%;
      text-align: center;
      margin-top: 168px;
    }
</style>
<style lang="less">
</style>
